package com.caishi.lkx.exam.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.exam.service.impl.AdvertisementPositionServiceImpl;
import com.caishi.lkx.user.model.AdvertisementPositionModel;
import com.caishi.lkx.user.service.IUserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author by liyihua
 * @date : 2022-01-06 14:29
 */

@RestController
@RequestMapping("/advertisement/position")
public class AdvertisementPositionApi extends AdminBaseApi<AdvertisementPositionServiceImpl, AdvertisementPositionModel, Long> {
    @Resource
    private AdvertisementPositionServiceImpl advertisementPositionService;
    @Resource
    private IUserService userService;

    @Override
    public AdvertisementPositionServiceImpl loadService() {
        return advertisementPositionService;
    }

    @Override
    protected LambdaQueryWrapper<AdvertisementPositionModel> baseWrapper(AdvertisementPositionModel model, JSONObject otherParam) {
        return super.baseWrapper(
                        model.clearEmptyStringField("name"), otherParam)
                .like(StrUtil.isNotBlank(model.getName()), AdvertisementPositionModel::getName, model.getName());
    }


    @Override
    protected List<AdvertisementPositionModel> listCover(List<AdvertisementPositionModel> result, JSONObject otherParam) {

        return result.parallelStream().peek(r->{
            r.setCreatedName(userService.getUserNameById(r.getCreatedBy()));
        }).collect(Collectors.toList());
    }
}
